<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.ArrayList,java.util.HashMap,java.net.URLDecoder,cn.edu.nju.ws.aquila.rdf.ResourceFactory" %>
<%@ page import="cn.edu.nju.ws.aquila.query.Datasource" %>
<%@ page import="cn.edu.nju.ws.aquila.query.Query" %>
<%@ page import="cn.edu.nju.ws.aquila.ui.browsing.URIBrowsing" %>
<%@ page import="cn.edu.nju.ws.aquila.rdf.RDFTriple" %>
<%@ page import="cn.edu.nju.ws.aquila.rdf.URIResource" %>
<%@ page import="cn.edu.nju.ws.aquila.rdf.Resource" %>
<%@ page import="cn.edu.nju.ws.aquila.rdf.Literal" %>
<%@ page import="cn.edu.nju.ws.aquila.rdf.BlankNode" %>
<%@ page import="java.net.URLEncoder" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Entity Browsing</title>
<link rel="stylesheet" type="text/css" href="css/layout.css" />
<link rel="stylesheet" type="text/css" href="css/browsing.css" />
<link rel="stylesheet" type="text/css" href="css/smoothness/jquery-ui-1.8.16.custom.css" />
<script type="text/javascript" src="script/jquery.js"></script>
<script type="text/javascript" src="script/construct_relation.js"></script>
<script type="text/javascript" src="script/jquery-ui-1.8.16.custom.min.js"></script>
</head>
<body>
		<div id="header">
			<div class="nav"><a href="index.jsp">Go to Aquila</a></div>
		</div>
		<div id="wrapper">
			<div id="top">
				<%
				 //客户端浏览某个URI信息
				   String queryIdStr = request.getParameter("id") ; 
				   int query_id = Integer.parseInt(queryIdStr) ;
				   String uriStr = request.getParameter("uri") ;
				   URLDecoder coder = new URLDecoder();
				   String uri = coder.decode(uriStr);
			       String label = ResourceFactory.getInstance().createURI(uri).getLabel();
			       String type =  ResourceFactory.getInstance().getResourceType(ResourceFactory.getInstance().createURI(uri));
			    	String typeLabel = null;
			    	if(type!=null){
			    		typeLabel =ResourceFactory.getInstance().createURI(type).getLabel();
			    	}
				%>
				<div  class="general_info">
					<ul>
						  <li class="entity-label"><%=label%></li>
						  <li class="entity-id"><%=uri%></li>
						  <%if(type!=null) {%>
						  <li class="entity-type">is a <span class="type" title="<%=type%>"><%=typeLabel%></span></li>
						  <%} %>
				  </ul>
				</div>
			</div>
			<%
			 //获取三元组
			 ArrayList<Datasource> dsArray = new  ArrayList<Datasource>();
			ArrayList<Datasource> dslist  = Query.getDatasourcesByQueryId(query_id) ;
			dsArray.addAll(dslist) ;
			 HashMap<URIResource,ArrayList<RDFTriple>> triples = URIBrowsing.getInstance().getTriples(ResourceFactory.getInstance().createURI(uri),dsArray);
			%>
			<div class="mspacingLittle"></div>
			<div id="main">
				<div id="property-list">
					 <ul>
					    <% 
					       for(URIResource prop:triples.keySet()){
					    	   String property = prop.getURI();
					           String propLabel = ResourceFactory.getInstance().createURI(property).getLabel();
                         %>
					    <li class='prop'>
					 	 	  <div class='titlebar'>
					 	 	  	 <span class='title' title='<%=property%>'><%=propLabel%></span>
					 	 	  </div>
					 	 	  <div class='content'>
					 	 	  	<ul class='pvalue'>
					 	 	  	<%
					 	 	  	//blank node不给它继续浏览的机会
					 	 	       ArrayList<RDFTriple> list = triples.get(prop) ;
					 	 	  	   for(RDFTriple triple:list){
					 	 	  		   Resource resource = triple.getObject() ;
					 	 	  		   if(resource instanceof URIResource){
					 	 	  			   URIResource uriResource = (URIResource)resource;
					 	 	  			   String ovalue = uriResource.getURI();
					 	 	  			   String olabel = uriResource.getLabel();	
					 	 	  			   String encodeVal = URLEncoder.encode(ovalue) ;
					 	 	  	%>
					 	 	  	<li>
					 	             <a href='browse_ui.jsp?id=<%=query_id%>&uri=<%=encodeVal%>' title='<%=ovalue%>'><%=olabel%></a>
					 	 	    </li>	
					 	 	     <%  } else{ 
					 	 	    	 String ovalue = null;
					 	 	    	 if(resource instanceof Literal){
					 	 	    		 ovalue =((Literal)resource).getLexicalForm();
					 	 	    	 }else
					 	 	    		 ovalue = ((BlankNode)resource).getLabel();
					 	 	     %>
							 	 	<li>
						 	            <%=ovalue%>
						 	 	    </li>  
					 	 	     <%}}%>
					 	 	     				 	 	    
					 	 	    </ul>
					 	 	  </div>					 	 	  
					   </li>	
					  <%} %> 					 				 	 	  				  					 
					 </ul>
			  </div>
			</div>
		</div>
	<div id="footer"></div>
</body>
</html>